package com.james.security;

import com.alibaba.fastjson.JSON;
import com.james.util.ResultUtil;
import com.james.view.ResultCode;
import com.james.view.ResultView;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ：zhz
 * @date ：2022/7/5 9:05
 * @description：TODO
 * @version: 1.0
 */
@Component
public class CustomizeAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        httpServletResponse.setStatus(HttpStatus.UNAUTHORIZED.value());
        ResultView resultView = ResultUtil.getInstanceViewOfNotData(ResultCode.USER_NOT_LOGIN);
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(resultView));
    }
}
